package com.nwork.cateye.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


import com.nwork.cateye.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by licheng  on 2015/3/13.
 * email 635408136@qq.com
 */
public class CommunityListViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> data;
    private ViewPager viewPager;
    private List<Integer> imgeIds;
    private int currentItem; //当前页面
    private ScheduledExecutorService scheduledExecutorService;

    public CommunityListViewAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (data != null) {
            return data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (data != null) {
            return data.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        //the first module//////////////////////////////////////////////////////////////
        if(position==0){
            view = LayoutInflater.from(context).inflate(R.layout.item_listview_top_community,parent,false);
            viewPager = (ViewPager) view.findViewById(R.id.communityviewPager);
            imgeIds = new ArrayList<Integer>();
          //  imgeIds.add(R.drawable.translate72);
                imgeIds.add(R.drawable.communityimg1);
                imgeIds.add(R.drawable.communityimg2);
                 imgeIds.add(R.drawable.communityimg3);
                  imgeIds.add(R.drawable.communityimg4);
                imgeIds.add(R.drawable.communityimg5);
              //    imgeIds.add(R.drawable.translate72);

            CommenImagePageAdapter adapter=new CommenImagePageAdapter(imgeIds,context);
            viewPager.setAdapter(adapter);
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
//                    if (imgeIds.size() > 1) { //多于1，才会循环跳转
//                        if (position < 1) { //首位之前，跳转到末尾（N）
//                            position = 5;
//                            viewPager.setCurrentItem(position, false);
//                        } else if (position > 5) { //末位之后，跳转到首位（1）
//                            viewPager.setCurrentItem(1, false); //false:不显示跳转过程的动画
//                            position = 1;
//                        }
//                    }
                    currentItem=position;

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

                    //每隔2秒钟切换一张图片
             scheduledExecutorService.scheduleWithFixedDelay(new ViewPagerTask(), 3, 3, TimeUnit.SECONDS);


        //the second module/////////////////////////////////////////////////////////
        }else if (position==1){
            view = LayoutInflater.from(context).inflate(R.layout.item_listview_middle_community,parent,false);

        }else
        {
            view = LayoutInflater.from(context).inflate(R.layout.item_listview_top_community,parent,false);
        }

        return view;
    }

    //切换图片
       private class ViewPagerTask implements Runnable {

                       @Override
                public void run() {
                      // TODO Auto-generated method stub
                       currentItem = (currentItem +1) % imgeIds.size();
                     //更新界面
             //            handler.sendEmptyMessage(0);
                      handler.obtainMessage().sendToTarget();
                  }

                    }

               private Handler handler = new Handler(){

                      @Override
                public void handleMessage(Message msg) {
                      // TODO Auto-generated method stub
                      //设置当前页面
                          viewPager.setCurrentItem(currentItem);
                 }

                   };

    private static class ViewHolder {


    }
}
